package com.top.mall.main.mine

import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.AreaList
import com.top.mall.jetpack.vm.MineViewModel
import kotlinx.android.synthetic.main.activity_address_save.*
import kotlinx.android.synthetic.main.activity_address_save.imageViewBack
import kotlinx.android.synthetic.main.activity_mine_profile.*

class AddressSaveActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(AddressSaveActivity::class.java)
        }
    }

    private var mAreaListProvince: AreaList? = null

    private var mAreaListCity: AreaList? = null

    private var mAreaListDistrict: AreaList? = null

    private val mOptionsProvince: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this) { options1, option2, options3, v ->
            val data = viewModel.userAreaLiveData.value?.data?.data ?: return@OptionsPickerBuilder
            mAreaListProvince = data[options1]
            tvAddressProvince.text = mAreaListProvince?.name

            tvAddressCity.text = null
            tvAddressDistrict.text = null
            mAreaListCity = null
            mAreaListDistrict = null
        }.build()
    }

    private val mOptionsCity: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this) { options1, option2, options3, v ->
            val data = viewModel.userAreaLiveData.value?.data?.data ?: return@OptionsPickerBuilder
            mAreaListCity = data[options1]
            tvAddressCity.text = mAreaListCity?.name

            tvAddressDistrict.text = null
            mAreaListDistrict = null
        }.build()
    }

    private val mOptionsDistrict: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this) { options1, option2, options3, v ->
            val data = viewModel.userAreaLiveData.value?.data?.data ?: return@OptionsPickerBuilder
            mAreaListDistrict = data[options1]
            tvAddressDistrict.text = mAreaListDistrict?.name
        }.build()
    }

    override val layoutResId: Int
        get() = R.layout.activity_address_save

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.colorWhite, view = flAddressBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        tvAddressSave.setOnClickListener {
            if (mAreaListProvince == null || mAreaListCity == null || mAreaListDistrict == null || tvAddressDetails.text.isNullOrEmpty()) {
                toast("请选择填写完整信息")
                return@setOnClickListener
            }

            viewModel.saveAddress(
                province = mAreaListProvince!!.id,
                city = mAreaListCity!!.id,
                district = mAreaListDistrict!!.id,
                address = tvAddressDetails.text.toString(),
                is_default = if (cbAddressSelect.isChecked) 1 else 0
            )
        }

        tvAddressProvince.setOnClickListener {
            viewModel.userArea(grade = 1)
        }
        tvAddressCity.setOnClickListener {
            if (mAreaListProvince == null) {
                toast("请选择省份")
                return@setOnClickListener
            }
            viewModel.userArea(province = mAreaListProvince!!.id, grade = 2)
        }
        tvAddressDistrict.setOnClickListener {
            if (mAreaListCity == null) {
                toast("请选择城市")
                return@setOnClickListener
            }
            viewModel.userArea(city = mAreaListCity!!.id, grade = 3)
        }
    }

    override fun initData() {

    }

    override fun initViewMode() {
        viewModel.saveAddressLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg)
            finish()
        })
        viewModel.userAreaLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            if (data.grade == 1) {
                mOptionsProvince.setPicker(data.data.map { it.name })
                mOptionsProvince.show()
            } else if (data.grade == 2) {
                mOptionsCity.setPicker(data.data.map { it.name })
                mOptionsCity.show()
            } else if (data.grade == 3) {
                mOptionsDistrict.setPicker(data.data.map { it.name })
                mOptionsDistrict.show()
            }
        })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }
}